﻿// WPF 实现可拖拽调整顺序的ListView自定义控件:
// https://blog.csdn.net/qq_42399932/article/details/139676322

using System.Collections.ObjectModel;
using System.Windows;
using System.Text;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace WpfCsvViewer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public ObservableCollection<Book> books = new ObservableCollection<Book>();
        GridViewColumnHeader _lastHeaderClicked = null;
        ListSortDirection _lastDirection = ListSortDirection.Ascending;

        public MainWindow()
        {
            InitializeComponent();
            initList();
        }

        public void initList()
        {
            var subjects = new string[] { "Math", "English", "Physics", "Chemistry", "C++", "VB", "Algorithms", "Java", "Html", "LLM" };
            Random random = new Random();
            for (int i = 0; i < 10; i++)
            {
                books.Add(new Book()
                {
                    book_id = i,
                    book_name = "" + random.Next(100),
                    book_type = subjects[i]
                });

            }
            lv.ItemsSource = books;
        }

        private void btnTest1_Click(object sender, RoutedEventArgs e)
        {
            lv.Items.SortDescriptions.Add(new SortDescription("book_name", ListSortDirection.Descending));
        }

        private void colID_MouseDown(object sender, MouseButtonEventArgs e)
        {
            lv.Items.SortDescriptions.Add(new SortDescription("book_id", ListSortDirection.Descending));
        }

        private void listView1_Click(object sender, RoutedEventArgs e)
        {
            var headerClicked = e.OriginalSource as GridViewColumnHeader;
            ListSortDirection direction;

            if (headerClicked != null)
            {
                if (headerClicked.Role != GridViewColumnHeaderRole.Padding)
                {
                    if (headerClicked != _lastHeaderClicked)
                    {
                        direction = ListSortDirection.Ascending;
                    }
                    else
                    {
                        if (_lastDirection == ListSortDirection.Ascending)
                        {
                            direction = ListSortDirection.Descending;
                        }
                        else
                        {
                            direction = ListSortDirection.Ascending;
                        }
                    }

                    var columnBinding = headerClicked.Column.DisplayMemberBinding as Binding;
                    var sortBy = columnBinding?.Path.Path ?? headerClicked.Column.Header as string;

                    Sort(sortBy + "", direction);

                    if (direction == ListSortDirection.Ascending)
                    {
                        headerClicked.Column.HeaderTemplate =
                          Resources["HeaderTemplateArrowUp"] as DataTemplate;
                    }
                    else
                    {
                        headerClicked.Column.HeaderTemplate =
                          Resources["HeaderTemplateArrowDown"] as DataTemplate;
                    }

                    // Remove arrow from previously sorted header
                    if (_lastHeaderClicked != null && _lastHeaderClicked != headerClicked)
                    {
                        _lastHeaderClicked.Column.HeaderTemplate = null;
                    }

                    _lastHeaderClicked = headerClicked;
                    _lastDirection = direction;
                }
            }
        }

        private void Sort(string sortBy, ListSortDirection direction)
        {
            ICollectionView dataView =
              CollectionViewSource.GetDefaultView(lv.ItemsSource);

            dataView.SortDescriptions.Clear();
            SortDescription sd = new SortDescription(sortBy, direction);
            dataView.SortDescriptions.Add(sd);
            dataView.Refresh();
        }

    }

    public class Book
    {
        public int book_id { get; set; } = 0;
        public string book_name { get; set; } = string.Empty;
        public string book_type { get; set; } = string.Empty;

        public string book_author = string.Empty;
        public string publisher = string.Empty;
        public double book_price = 0;
        public string book_status = string.Empty;

        public Book() { }

        public Book(int id, string name, string type)
        {
            book_id = id;
            book_name = name;
            book_type = type;
        }

    }
}